package com.example.demo.interceptor;

import com.example.demo.util.TokenUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        System.out.println("preHandle");
        String url = request.getRequestURI();
        System.out.println("当前路径"+url);
        if("/login".equals(url)){
            return true;
        }else {
            System.out.println("111");
            String token = request.getHeader("token");
            if (!TokenUtil.verify(token)) {
                System.out.println(token);
                throw new RuntimeException("no login");
            } else {
                return true;
            }
        }
    }

}
